var Util = {
    f2j: function ($this) {
        var $els = $this.find('input[name], select[name], textarea[name]:enabled,i.object[name]'), //
            arrs = [];

        while ($els.length > 0) {
            var $el = $els.eq(0);
            if ($el.is('input[type=text]') || $el.is('input[type=hidden]') || $el.is('textarea')) {
                arrs.push({
                    name: $el.attr('name'),
                    value: $.trim($el.val())
                });
                $els = $els.not($el);
            } else if ($el.is('input[type=checkbox]')) {
                var name = $el.attr('name'), //
                    $checkbox = $els.filter('[name="' + name + '"]'), //
                    $checked = $checkbox.filter(':checked');
                vals = [];
                for (var i = 0; i < $checked.length; i++) {
                    vals.push($checked.eq(i).val());
                }
                arrs.push({
                    name: name,
                    value: vals
                });
                $els = $els.not($checkbox);
            } else if ($el.is('input[type=radio]')) {
                var name = $el.attr('name'), //
                    $radio = $els.filter('[name="' + name + '"]');
                arrs.push({
                    name: name,
                    value: $radio.filter(':checked').val()
                });
                $els = $els.not($radio);
            } else if ($el.is('select')) {
                arrs.push({
                    name: $el.attr('name'),
                    value: $el.val()
                });
                $els = $els.not($el);
            } else if ($el.is('i.object')) {
                var name = $el.attr('name'),
                    $checkbox = $els.filter('[name="' + name + '"]'),
                    $checked = $checkbox.filter('.check');
                var vals = [];
                for (var i = 0; i < $checked.length; i++) {
                    vals.push($checked.eq(i).attr('data-id'));
                }
                arrs.push({
                    name: name,
                    value: vals
                });
                $els = $els.not($checkbox);
            } else {
                //console.log('解析错误', $els);
                $els = $els.not($el);
            }
        }

        //return arrs;
        return F2J.translate(arrs);
    },

    j2f: function ($this, data) {
        $els = $this.find("[name]");
        if (typeof data === "string") {
            data = JSON.parse(data);
        }
        J2F.parse($els, data);
        return $this;
    },


    /**
     * 创建表单元素
     * @param url 需要提交的url
     * @param data 提交的数据
     * @param method 方法
     * @returns {*}
     */
    form: function (url, data, method) {
        if (method == null) method = 'POST';
        if (data == null) data = {};

        var form = $('<form>').attr({
            method: method,
            action: url
        }).css({
            display: 'none'
        });

        var addData = function (name, data) {
            if ($.isArray(data)) {
                for (var i = 0; i < data.length; i++) {
                    var value = data[i];
                    addData(name + '[]', value);
                }
            } else if (typeof data === 'object') {
                for (var key in data) {
                    if (data.hasOwnProperty(key)) {
                        addData(name + '[' + key + ']', data[key]);
                    }
                }
            } else if (data != null) {
                form.append($('<input>').attr({
                    type: 'hidden',
                    name: String(name),
                    value: String(data)
                }));
            }
        };

        for (var key in data) {
            if (data.hasOwnProperty(key)) {
                addData(key, data[key]);
            }
        }

        return form.appendTo('body');
    }
};
//$.fn.Util = function () {
//options = $.extend(defaults, options || {});

//return new Util();
//}
//return new Util();
//})(jQuery);

F2J = {
    FORM_PATTERN: /.+\[\d*\]+$/,
    generate: function (obj, name, value) {
        if (name && name.length) {
            if (obj[name] === undefined) {
                obj[name] = value;
            } else {
                if (!obj[name].push) {
                    obj[name] = [obj[name]];
                }
                obj[name].push(value);
            }
        }
        return obj;
    },
    translate: function (arrs) {
        var obj = {};
        $.each(arrs, function () {
            if (this.name.indexOf('.') != -1) {
                var params = this.name.split('.'), //
                    innerobj = obj;
                for (var i = 0; i < params.length - 1; i++) {
                    var index = -1, //
                        name = params[i], //
                        tobj;
                    if (F2J.FORM_PATTERN.test(name)) {
                        index = name.substring(name.indexOf('[') + 1, name.indexOf(']'));
                        name = name.substring(0, name.indexOf('['));
                    }
                    tobj = innerobj[name];
                    // 未定义，则初始化
                    if (tobj === undefined) {
                        if (-1 === index) {
                            innerobj[name] = {};
                        } else {
                            innerobj[name] = [];
                        }
                    }
                    if (-1 === index) {
                        innerobj = innerobj[name];
                    } else {
                        if (!innerobj[name][index]) {
                            innerobj[name][index] = {};
                        }
                        innerobj = innerobj[name][index];
                    }
                }
                F2J.generate(innerobj, params[params.length - 1], this.value);
            } else {
                F2J.generate(obj, this.name, this.value);
            }
        });
        return obj;
    }
}


